﻿using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_ProcecureBusiness : BaseBusiness<PB_Procecure>, IPB_ProcecureBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public PB_ProcecureBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<PB_ProcecureDTO>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var search = input.Search;
            Expression<Func<PB_Procecure, Base_Department, PB_ProcecureDTO>> select = (a, b) => new PB_ProcecureDTO
            {
                DepartmentName = b.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.DeptId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    select @select.Invoke(a, b);
            if (!search.Keyword.IsNullOrEmpty())
            {
                var keyword = $"%{search.Keyword}%";
                q = q.Where(x =>
                      EF.Functions.Like(x.Name, keyword)
                      || EF.Functions.Like(x.Code, keyword));
            }
            q = q.Where(m => _operator.Property.DeptIds.Contains(m.DeptId));
            return await q.GetPageResultAsync(new PageInput());
        }

        public async Task<PageResult<PB_Procecure>> GetDataListByDepartmentAsync(string DeptId)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_Procecure>();
            
            //筛选
            if (!DeptId.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_Procecure, bool>(
                    ParsingConfig.Default, false, $@"DeptId.Equals(@0)", DeptId);
                where = where.And(newWhere);
            }
            where = where.And(m=>_operator.Property.DeptIds.Contains(m.DeptId));
            return await q.Where(where).GetPageResultAsync(new PageInput());
        }

        public async Task<PB_Procecure> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        [DataAddLog(UserLogType.工序管理, "Name", "工序管理")]
        [DataRepeatValidate(new string[] { "Code", "Name" }, new string[] { "工序编号", "工序名称" })]
        public async Task AddDataAsync(PB_Procecure data)
        {
            await InsertAsync(data);
        }

        [DataAddLog(UserLogType.工序管理, "Name", "工序管理")]
        [DataRepeatValidate(new string[] { "Code", "Name" }, new string[] { "工序编号", "工序名称" })]
        public async Task UpdateDataAsync(PB_Procecure data)
        {
            await UpdateAsync(data);
        }

        [DataAddLog(UserLogType.工序管理, "Name", "工序管理")]
        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public Task AddDataAsyncs(PB_ProcecureInputDTO input)
        {
            throw new NotImplementedException();
        }

        public Task UpdateDataAsyncs(PB_ProcecureInputDTO input)
        {
            throw new NotImplementedException();
        }

        public Task<List<PB_ProcecureDTO>> GetDataListAsync(PB_ProcecureInputDTO input)
        {
            throw new NotImplementedException();
        }

        public Task<List<PB_ProcecureDTO>> GetTreeDataListAsync(PB_ProcecureInputDTO input)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region 私有成员

        #endregion
    }
}